/*
 * Copyright (c) $today.year-Now http://www.zxpnet.com All rights reserved.
 */

package com.zxpnet.demo.mybatisplus.enums;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 1正常 2禁用 3冻结）
 * @author: shollin
 * @date: 2021/6/28/028 22:28
 */
@Getter
@AllArgsConstructor
public enum UserState implements ReadableEnum<UserState> {

    NORMAL(1,"正常"),
    FORBBEN(2,"禁用"),
    CLOSED(3,"禁用");

    @EnumValue
    @JsonValue // 序列化时，将code返回给前端, 而不是枚举的名称  也可以写在description上
    private final Integer code;

    private final String message;

    /**
     * 根据code获取枚举
     *
     * @author fengshuonan
     * @since 2020/10/29 18:59
     */
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static UserState codeToEnum(Integer code) {
        if (null != code) {
            for (UserState item : UserState.values()) {
                if (item.getCode().equals(code)) {
                    return item;
                }
            }
        }
        return null;
    }

    @Override
    public Object getKey() {
        return this.code;
    }

    @Override
    public Object getName() {
        return this.message;
    }

    @Override
    public UserState parseToEnum(String originValue) {
        if (ObjectUtil.isEmpty(originValue)) {
            return null;
        }
        for (UserState value : UserState.values()) {
            if (value.code.equals(Convert.toInt(originValue))) {
                return value;
            }
        }
        return null;
    }


}
